package labox.innovation.gameserver.model;

import java.util.List;

import javolution.util.FastList;
import labox.innovation.gameserver.model.actor.L2Attackable;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.CreatureSay;
//import labox.innovation.gameserver.network.serverpackets.ExCloseMPCC;
//import labox.innovation.gameserver.network.serverpackets.ExMPCCPartyInfoUpdate;
//import labox.innovation.gameserver.network.serverpackets.ExOpenMPCC;
import labox.innovation.gameserver.network.serverpackets.L2GameServerPacket;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 *
 * @author  chris_00
 */
public class L2CommandChannel {
	
	private List<L2Party> _partys = null;

	private FuzePcInstance _commandLeader = null;

	private int _channelLvl;

	/**
	 * Creates a New Command Channel and Add the Leaders party to the CC
	 *
	 * @param CommandChannelLeader
	 *
	 */
	public L2CommandChannel(FuzePcInstance leader) {
		_commandLeader = leader;
		_partys = new FastList<L2Party>();
		_partys.add(leader.getParty());
		_channelLvl = leader.getParty().getLevel();
		leader.getParty().setCommandChannel(this);
		leader.getParty().broadcastToPartyMembers(new SystemMessage(SystemMessageId.COMMAND_CHANNEL_FORMED));
//		leader.getParty().broadcastToPartyMembers(new ExOpenMPCC());
	}

	/**
	 * Adds a Party to the Command Channel
	 * @param Party
	 */
	public void addParty(L2Party party) {
		if (party == null)
			return;
		// Update the CCinfo for existing players
//		broadcastToChannelMembers(new ExMPCCPartyInfoUpdate(party, 1));

		_partys.add(party);
		if (party.getLevel() > _channelLvl)
			_channelLvl = party.getLevel();
		party.setCommandChannel(this);
		party.broadcastToPartyMembers(new SystemMessage(SystemMessageId.JOINED_COMMAND_CHANNEL));
//		party.broadcastToPartyMembers(new ExOpenMPCC());
	}

	/**
	 * Removes a Party from the Command Channel
	 * @param Party
	 */
	public void removeParty(L2Party party) {
		if (party == null)
			return;

		_partys.remove(party);
		_channelLvl = 0;
		for (L2Party pty : _partys) {
			if (pty.getLevel() > _channelLvl)
				_channelLvl = pty.getLevel();
		}
		party.setCommandChannel(null);
//		party.broadcastToPartyMembers(new ExCloseMPCC());
		if (_partys.size() < 2) {
			broadcastToChannelMembers(new SystemMessage(SystemMessageId.COMMAND_CHANNEL_DISBANDED));
			disbandChannel();
		} else {
			// Update the CCinfo for existing players
//			this.broadcastToChannelMembers(new ExMPCCPartyInfoUpdate(party, 0));
		}
	}

	/**
	 * disbands the whole Command Channel
	 */
	public void disbandChannel() {
		if (_partys != null) {
			for (L2Party party : _partys) {
				if (party != null)
					removeParty(party);
			}
		}
		_partys = null;
	}

	/**
	 * @return overall membercount of the Command Channel
	 */
	public int getMemberCount() {
		int count = 0;
		for (L2Party party : _partys) {
			if (party != null)
				count += party.getMemberCount();
		}
		return count;
	}

	/**
	 * Broadcast packet to every channelmember
	 * @param L2GameServerPacket
	 */
	public void broadcastToChannelMembers(L2GameServerPacket gsp) {
		if (_partys != null && !_partys.isEmpty()) {
			for (L2Party party : _partys) {
				if (party != null)
					party.broadcastToPartyMembers(gsp);
			}
		}
	}

	public void broadcastCSToChannelMembers(CreatureSay gsp, FuzePcInstance broadcaster) {
		if (_partys != null && !_partys.isEmpty()) {
			for (L2Party party : _partys) {
				if (party != null)
					party.broadcastCSToPartyMembers(gsp, broadcaster);
			}
		}
	}

	/**
	 * @return list of Parties in Command Channel
	 */
	public List<L2Party> getPartys() {
		return _partys;
	}

	/**
	 * @return list of all Members in Command Channel
	 */
	public List<FuzePcInstance> getMembers() {
		List<FuzePcInstance> members = new FastList<FuzePcInstance>();
		for (L2Party party : getPartys()) {
			members.addAll(party.getPartyMembers());
		}
		return members;
	}

	/**
	 *
	 * @return Level of CC
	 */
	public int getLevel() {
		return _channelLvl;
	}

	/**
	 * @param sets the leader of the Command Channel
	 */
	public void setChannelLeader(FuzePcInstance leader) {
		_commandLeader = leader;
	}

	/**
	 * @return the leader of the Command Channel
	 */
	public FuzePcInstance getChannelLeader() {
		return _commandLeader;
	}

	/**
	 * Queen Ant, Core, Orfen, Zaken: MemberCount > 36<br>
	 * Baium: MemberCount > 56<br>
	 * Antharas: MemberCount > 225<br>
	 * Valakas: MemberCount > 99<br>
	 * normal RaidBoss: MemberCount > 18
	 *
	 * @param obj
	 * @return true if proper condition for RaidWar
	 */
	public boolean meetRaidWarCondition(L2Object obj) {
		if (!(obj instanceof L2Character && ((L2Character) obj).isRaid()))
			return false;
		int npcId = ((L2Attackable) obj).getNpcId();
		switch (npcId) {
		case 29001: // Queen Ant
		case 29006: // Core
		case 29014: // Orfen
		case 29022: // Zaken
			return (getMemberCount() > 36);
		case 29020: // Baium
			return (getMemberCount() > 56);
		case 29019: // Antharas
			return (getMemberCount() > 225);
		case 29028: // Valakas
			return (getMemberCount() > 99);
		case 29045: // Frintezza
			return (getMemberCount() > 35);
		default: // normal Raidboss
			return (getMemberCount() > 18);
		}
	}
}
